package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.AlgorithmPadding;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.*;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyYuanQin;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 安心-融汇-清远四选20crm
 *
 * @author 李源聪
 */
@ApiPlan(productId = "401", type = ApiPlanConvertType.ORG_PHONE_MASK)
public class ApiOrgPhoneMaskYuanQin implements ApiOrgPhoneMaskConvertHandler {
    private static final Integer FILED_ID = 52;
    private static final String KEY = "gQ0x679TDY8tMxgZ";
    private static final String PROMOTION = "5202";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        Map<Object, Object> dataMap = new LinkedHashMap<>();
        dataMap.put("mobile", apiDataExchangeParameter.getPhoneMask());
        AES ecb = AES.createEcb(KEY.getBytes(), AlgorithmPadding.PKCS5);
        String json = JsonUtils.toJson(dataMap);
        String encryptData = ecb.encryptToBase64Str(json);
        return MapUtils.newHashMap(newMap -> newMap.put("data", encryptData));
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("code"), 0);
        if (!code) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        //  获取手机号掩码
        List<String> listPhoneMd5 = CollUtils.newArrayList(newList -> {
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            IteratorUtils.array(jsonArray, (item, index) -> {
                String phoneMd5 = jsonArray.getString(index);
                newList.add(phoneMd5);
            });
        });
        RespOrgPhoneMaskDataExchange respOrgPhoneMaskDataExchange = RespOrgPhoneMaskDataExchange.ofSuc();
        respOrgPhoneMaskDataExchange.setListPhoneMd5(listPhoneMd5);
        return respOrgPhoneMaskDataExchange;
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        apiApplyParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        OrgApplyYuanQin applyLongYanQiNuoHangZhou = new OrgApplyYuanQin();
        //拓展
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //file_id
        applyLongYanQiNuoHangZhou.setFile_id(FILED_ID);
        applyLongYanQiNuoHangZhou.setPromotion(PROMOTION);
        //姓名
        applyLongYanQiNuoHangZhou.setName(apiApplyParameter.getUserName());
        //手机号码
        applyLongYanQiNuoHangZhou.setMobile(apiApplyParameter.getPhone());
        //年龄(若没有传0)
        applyLongYanQiNuoHangZhou.setAge(apiFieldConverter.getAgeInt());
        //性别(不传默认0。男传0、女传1)
        Integer newSex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 0)
                .when(apiApplyParameter.getSex(), "1", 1)
                .end(0);
        applyLongYanQiNuoHangZhou.setSex(newSex);
        //城市(如天津市)
        applyLongYanQiNuoHangZhou.setCity(apiApplyParameter.getCity());
        //是否有房(是传1,否传0)
        applyLongYanQiNuoHangZhou.setIs_house(apiFieldConverter.hasRealEstateInfoToNumber());
        //是否有车(是传1,否传0)
        applyLongYanQiNuoHangZhou.setIs_car(apiFieldConverter.hasCarInfoToNumber());
        //是否有公司营业执照(是传1,否传0)
        applyLongYanQiNuoHangZhou.setIs_company(apiFieldConverter.hasBizLicenseYearToNumber());
        //是否有信用卡 (是传1,否传0)
        applyLongYanQiNuoHangZhou.setIs_credit(apiFieldConverter.hasCreditLimitToNumber());
        //是否有保单(是传1,否传0)
        applyLongYanQiNuoHangZhou.setIs_insurance(apiFieldConverter.hasInsurancePolicyToNumber());
        //是否有社保(是传1,否传0)
        applyLongYanQiNuoHangZhou.setIs_social(apiFieldConverter.hasSocialSecurityToNumber());
        //是否有公积金(是传1,否传0)
        applyLongYanQiNuoHangZhou.setIs_fund(apiFieldConverter.hasProvidentFundToNumber());
        //是否有打卡工资(是传1,否传0)
        Integer newWork = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                .when(apiApplyParameter.getSalaryPayMethod(), "2", 0)
                .when(apiApplyParameter.getSalaryPayMethod(), "3", 0)
                .end(0);
        applyLongYanQiNuoHangZhou.setIs_work(newWork);
        //是否有营业税(是传1,否传0)
        applyLongYanQiNuoHangZhou.setIs_tax(0);
        //是否有微粒贷(是传1,否传0)
        applyLongYanQiNuoHangZhou.setWebank(0);
        //申请额度（单位:元）
        applyLongYanQiNuoHangZhou.setMoney_demand(apiFieldConverter.getLoanAmountInt());
        AES ecb = AES.createEcb(KEY.getBytes(), AlgorithmPadding.PKCS5);
        String json = JsonUtils.toJson(applyLongYanQiNuoHangZhou);
        String encryptData = ecb.encryptToBase64Str(json);
        return MapUtils.newHashMap(newMap -> newMap.put("data", encryptData));
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("code"), 0);
        if (!code) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}
